﻿using System;
using System.Collections.Generic;

namespace IceCS.Puke.Core
{
    /// <summary>
    /// 卡片列表对象
    /// </summary>
    public class CardList : List<Card>, ICloneable
    {
        public CardList()
        {

        }
        /// <summary>
        ///  
        /// </summary>
        /// <param name="capacity"></param>
        public CardList(int capacity) : base(capacity)
        {

        }
        /// <summary>
        /// 把当前的cardList 复制到另一个cardList
        /// </summary>
        /// <param name="cardList">目标 cardList 对象</param>
        public void CopyTo(CardList cardList)
        {
            for (int i = 0; i < Count; i++)
            {
                cardList[i] = this[i];
            }
        }

        /// <summary>
        /// 进行打乱排序操作
        /// </summary>
        public void Shuffle()
        {
            CardList newCardList = new CardList(Count);  //创建一个新的Card对象集合
            bool[] assigned = new bool[Count]; //是否已分配卡牌的flag，默认是false
            Random sourceGen = new Random();

            for (int i = 0; i < 52; i++)
            {
                int destCard = 0;
                bool foundCard = false;
                while (foundCard == false)
                {
                    //随机找个目标位置
                    destCard = sourceGen.Next(52);
                    //如果目标位置没有卡牌占用，就把当前卡牌放到那个位置去
                    if (!assigned[destCard])
                        foundCard = true;
                }
                assigned[destCard] = true;
                newCardList.Add(this[destCard]);
            }
            //把洗好的卡牌复制回原来的数组
            newCardList.CopyTo(this);
        }

        #region 实现 ICloneable 接口
        /// <summary>
        /// 创建作为当前实例副本的新对象。
        /// </summary>
        /// <returns>作为此实例副本的新对象。</returns>
        public object Clone()
        {
            var list = new CardList();
            foreach (Card card in this)
            {
                list.Add(card.Clone() as Card);
            }
            return list;
        }
        #endregion
    }
}
